﻿package DevilCat{
	import flash.display.MovieClip;
	import flash.net.SharedObject;
	import flash.text.TextField;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import flash.display.SimpleButton;    
	import flash.utils.getDefinitionByName;
	import DevilCat.SQLData;
	import DevilCat.PokemonObject;
	import DevilCat.ItemObject;
	import DevilCat.FloatingText;

	public class PokemonBattle extends MovieClip {
		private var eventLocation:Number=0;
		private var eventNumber:Number=0;
		
		private var tweenList:Array;
		private var myPokemonInList:Number=0;
		private var targetPokemonList:Array;
		private var myPokemon:PokemonObject;
		private var targetPokemon:PokemonObject;
		private var allMyPokemonFailed:Boolean=false;
		private var allTargetPokemonFailed:Boolean=false;
		private var catched:Boolean=false;
		private var manualSwitch:Boolean=true;
		private var isTrainer:Boolean=false;
		private var endBattleFuntion:Function;
		private var moveIsShowed:Boolean=false;
		private var itemIsShowed:Boolean=false;
		private var pokemonIsShowed:Boolean=false;
		private var itemList:Array;
		private var floatSpace:Number=17;
		private var floatTime:Number=2;
		private var weather:String="None";
		private var weatherTurn:Number=-1;
		
		private var myOldHP:Number=0;

		public function PokemonBattle(_targetPokemonList:Array,_endBattleFuntion:Function,_isTrainer:Boolean=false,addStat:Number=0) {
			SQLData.battleClip=this;
			
			itemList=SQLData.itemList;			
			
			endBattleFuntion=_endBattleFuntion;
			targetPokemonList=_targetPokemonList;
			isTrainer=_isTrainer;
			targetPokemon=targetPokemonList[0];
			
			var RandomStat:Number=Math.round(Math.random()*20)-10+addStat;
			for(var i:int=0; i<targetPokemonList.length; i++){
				targetPokemonList[i].addBonus(RandomStat,-RandomStat,RandomStat,RandomStat,-RandomStat,RandomStat);				
			}
			Balls.alpha=0;			
			enableThrow();
			recoveryPokemon();
			myPokemonInList=checkAllPokemonFailed(SQLData.pokemonList);
			myPokemon=SQLData.pokemonList[myPokemonInList];
			loadItem();
			loadPokemonList();
			freeTween();
			tweenList.unshift( new Tween (this,"alpha",Elastic.easeOut,0,1,1,true));
			loadPokemonBattle();
			enableBattle();
			showMovePanel();
			SQLData.mainClip.hideAllPanel();
		}
		////////////////////////////////////////BATTLE////////////////////////////////////////////
		public function setEventBattle(eventLocationB:Number,eventNumberB:Number){			
			if(eventLocationB>0){
				eventLocation=eventLocationB;
				eventNumber=eventNumberB;			
			}
		}
		//---------------------LOAD 
		public function recoveryPokemon() {
			for (var i:int=0; i<SQLData.pokemonList.length; i++) {
				SQLData.pokemonList[i].recoveryBeforeBattle();
				SQLData.pokemonList[i].setStatusBeforeBattle();
			}
		}
		private function loadMyStatus() {
			loadMoves();//Load Moves
			RegeneratorAbility(myPokemon);
			MyPokemonHPBar.scaleX=myPokemon.HP/myPokemon.MaxHP;
			MyPokemonName.text=myPokemon.Name;
			if (myPokemon.Status!="") {
				MyPokemonName.text=myPokemon.Name+" "+myPokemon.Status;
			}
			MyPokemonHP.text=String(myPokemon.HP);
			MyPokemonTotal.text=String(myPokemon.Total);
			if (myPokemon.Ability=="Intimidate") {
				IntimidateAbility(targetPokemon);
			}
			loadPokemonImage(myPokemon.pid,-1);
		}
		private function loadTargetStatus() {
			RegeneratorAbility(targetPokemon);
			TargetPokemonHPBar.scaleX=targetPokemon.HP/targetPokemon.MaxHP;
			TargetPokemonName.text=targetPokemon.Name;
			if (targetPokemon.Status!="") {
				TargetPokemonName.text=targetPokemon.Name+" "+targetPokemon.Status;
			}
			TargetPokemonHP.text=String(targetPokemon.HP);
			TargetPokemonTotal.text=String(targetPokemon.Total);
			if (targetPokemon.Ability=="Intimidate") {
				IntimidateAbility(myPokemon);
			}
			loadPokemonImage(targetPokemon.pid,1);

		}
		private function weatherStatEffect(){
			WeatherAbility(myPokemon,"Rain","SwiftSwim","Speed",2);
			WeatherAbility(myPokemon,"Sun","Chlorophyll","Speed",2);
			WeatherAbility(targetPokemon,"Rain","SwiftSwim","Speed",2);
			WeatherAbility(targetPokemon,"Sun","Chlorophyll","Speed",2);			
		}
		private function IntimidateAbility(pokemon:PokemonObject) {
			if (pokemon.Ability !="ClearBody" && pokemon.Ability !="HyperCutter" && pokemon.Ability !="WhiteSmoke") {
				addChild(new FloatingText(pokemon.Ability,450,115));
				pokemon.setNewTemStat("Attack",pokemon.TemAttack*0.8);
			}
		}
		private function RegeneratorAbility(pokemon:PokemonObject) {
			if (pokemon.Ability =="Regenerator") {
				addChild(new FloatingText(pokemon.Ability,450,115));
				pokemon.HP+=Math.round(pokemon.MaxHP/3);
				pokemon.setMaxHP();
			}
		}
		private function WeatherAbility(pokemon:PokemonObject,wea:String, Abi:String, StatAbi:String, sBoot:Number) {
			if(pokemon.Ability==Abi){
				if (!pokemon.weatherAbi && weather==wea) {
					addChild(new FloatingText(pokemon.Ability,580,115));
					pokemon.setNewTemStat(StatAbi,pokemon["Tem"+StatAbi]*sBoot);
					pokemon.weatherAbi=true;
				}
				if (weather!=wea) {
					pokemon.setNewTemStat(StatAbi,pokemon["Tem"+StatAbi]);
					pokemon.weatherAbi=false;
				}
			}
		}
		public function checkAllPokemonFailed(PokemonList:Array):Number {
			for (var i:int=0; i<PokemonList.length; i++) {
				if (PokemonList[i].HP>0) {
					return i;
				}
			}
			return -1;
		}

		private function freeTween() {
			tweenList=null;
			tweenList=new Array();
		}
		private function loadPokemonBattle() {
			while (LoadPokemonImage.numChildren > 0) {
				LoadPokemonImage.removeChildAt(0);
			}
			loadMyStatus();
			loadTargetStatus();
			weatherStatEffect();
		}
		private function loadPokemonImage(pid:Number,scale:Number) {
			alpha=1;
			var McLoader:MovieClip = new MovieClip();
			var imageLoader:Loader = new Loader();
			var imageLink:URLRequest = new URLRequest("http://dex.poke-mega.org/image/pokemon/"+pid+".png");
			imageLoader.load(imageLink);
			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageLoaded);
			function imageLoaded(e:Event):void {
				McLoader.alpha=1;
				McLoader.addChild(imageLoader);
				McLoader.scaleX=scale;
				if (scale>0) {
					McLoader.x=200;
				} else {
					McLoader.x=60;
				}
				LoadPokemonImage.addChild(McLoader);
			}
		}
		//---------------------EVENT
		private function disableBattle() {
			disableMenu();
			disableThrow();
		}
		private function Flee(e:Event) {
			disableMenu();
			removeBattle();
		}
		private function removeBattle() {
			tweenList.unshift(new Tween (this,"alpha",Regular.easeOut,1,0,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,backToGame);
		}

		private function enableBattleEvent(evt:Event) {
			enableBattle();
		}
		private function enableBattle() {			
			SQLData.UpdatePokemonSQL(myPokemon);
			if (checkAllPokemonFailed(SQLData.pokemonList)<0) {
				trace("All My Pokemon Faint");
				allMyPokemonFailed=true;
			}
			if (checkAllPokemonFailed(targetPokemonList)<0) {
				trace("All Target Pokemon Faint");
				allTargetPokemonFailed=true;
			}
			
			if(myPokemon.HP>0&&targetPokemon.HP==0){
				pokemonGetExp();
			}
			if (allMyPokemonFailed||allTargetPokemonFailed) {
				removeBattle();
			} else if(myPokemon.HP==0 || targetPokemon.HP==0){
				if(myPokemon.HP==0){
					trace("myPokemon.HP==0 -->SwitchPokemon");
					showPokemonPanel();
					manualSwitch=false;
					enablePokemonSwitch();
				}
				if(targetPokemon.HP==0){
					targetPokemon=targetPokemonList[checkAllPokemonFailed(targetPokemonList)];					
					loadPokemonBattle();
					enableBattle();
				}
			} else {				
				enableMenu();
				enableThrow();
			}
		}
		private function backToGame(e:Event) {

			trace("backToGame");
			if (this.parent != null) {
				if (allTargetPokemonFailed && myPokemon.HP>0) {
					endBattleFuntion(false);
					this.parent.addChild(new FloatingText("You Win!",310,155,2));
					if(eventLocation>0){
						SQLData.changeUserEvent(eventLocation,eventNumber);
					}
					SQLData.changePMP(1);
				} else {
					if (allMyPokemonFailed) {
						endBattleFuntion(true);
						this.parent.addChild(new FloatingText("All Pokemon Had Failed",310,155,2));
					} else {
						if(catched){
							endBattleFuntion(false,true);
							this.parent.addChild(new FloatingText("----//Catched!",310,155,2));							
						}else{
							endBattleFuntion(false);
							this.parent.addChild(new FloatingText("Flee!",310,155,2));
						}
					}
				}
				
				this.parent.removeChild(this);
			}
		}
		private function pokemonGetExp(){
			var countx:Number=0;
			if(myPokemon.MaxTotal-targetPokemon.MaxTotal<150){
				if (targetPokemon.Exp.HP>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.HP+" HP",310,100-countx*15,2));
					countx++;
				}
				if (targetPokemon.Exp.AT>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.AT+" Attack",310,100-countx*15,2));
					countx++;
				}
				if (targetPokemon.Exp.DF>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.DF+" Defense",310,100-countx*15,2));
					countx++;
				}
				if (targetPokemon.Exp.SA>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.SA+" SpAtk",310,100-countx*15,2));
					countx++;
				}
				if (targetPokemon.Exp.SD>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.SD+" SpDef",310,100-countx*15,2));
					countx++;
				}
				if (targetPokemon.Exp.SP>0) {
					this.parent.addChild(new FloatingText("+ "+targetPokemon.Exp.SP+" Speed",310,100-countx*15,2));
					countx++;
				}
				myPokemon.getEXP(targetPokemon.Exp.HP,targetPokemon.Exp.AT,targetPokemon.Exp.DF,targetPokemon.Exp.SA,targetPokemon.Exp.SD,targetPokemon.Exp.SP);
			}else{				
				this.parent.addChild(new FloatingText("No Exp",310,100-countx*15,2));
			}
		}
		//---------------------MENU
		private function enableMenu() {
			ShowMove.addEventListener(MouseEvent.CLICK,showMovePanelEvent);
			ShowPokemon.addEventListener(MouseEvent.CLICK,showPokemonPanelEvent);
			ShowItem.addEventListener(MouseEvent.CLICK,showItemPanelEvent);
			if(!isTrainer)OutBattle.addEventListener(MouseEvent.CLICK,Flee);
			enableMoves();
			enableItem();
		}
		private function disableMenu() {
			ShowMove.removeEventListener(MouseEvent.CLICK,showMovePanelEvent);
			ShowPokemon.removeEventListener(MouseEvent.CLICK,showPokemonPanelEvent);
			ShowItem.removeEventListener(MouseEvent.CLICK,showItemPanelEvent);
			if(!isTrainer)OutBattle.removeEventListener(MouseEvent.CLICK,Flee);
			disableMoves();
			disableItem();
		}
		private function hideAllPanel() {
			hideMovePanel();
			hideItemPanel();
			hidePokemonPanel();
		}
		private function showMovePanel() {
			if (!moveIsShowed) {
				hideAllPanel();
				tweenList.unshift(new Tween (MovePanel,"y",Strong.easeOut,-272,-2,1,true));
				moveIsShowed=true;
				enableMoves();
			} else {
				hideMovePanel();
			}
		}
		private function showMovePanelEvent(e:MouseEvent) {
			showMovePanel();
		}
		private function hideMovePanel() {
			if (moveIsShowed) {
				disableMoves();				
				tweenList.unshift( new Tween (MovePanel,"y",Strong.easeOut,-2,-272,1,true));
				moveIsShowed=false;
			}
		}
		private function showPokemonPanelEvent(e:MouseEvent) {
			manualSwitch=true;
			showPokemonPanel();
		}
		private function showPokemonPanel() {
			loadPokemonList();
			if (!pokemonIsShowed) {
				hideAllPanel();				
				tweenList.unshift(new Tween (PokemonPanel,"y",Strong.easeOut,-272,-2,1,true));
				pokemonIsShowed=true;
				enablePokemonSwitch();
			} else {
				hidePokemonPanel();
			}
		}
		private function hidePokemonPanel() {
			if (pokemonIsShowed) {
				
				tweenList.unshift( new Tween (PokemonPanel,"y",Strong.easeOut,-2,-272,1,true));
				pokemonIsShowed=false;
				disablePokemonSwitch();
			}
		}
		private function showItemPanelEvent(e:MouseEvent) {
			showItemPanel();
		}
		private function showItemPanel() {
			if (!itemIsShowed) {
				hideAllPanel();				
				tweenList.unshift(new Tween (ItemPanel,"y",Strong.easeOut,-272,-2,1,true));
				itemIsShowed=true;
				enableItem();
			} else {
				hideItemPanel();
			}
		}
		private function hideItemPanel() {
			if (itemIsShowed) {
				disableItem();
				
				tweenList.unshift( new Tween (ItemPanel,"y",Strong.easeOut,-2,-272,1,true));
				itemIsShowed=false;
			}
		}

		//---------------------MOVES
		private function loadMoves() {
			for (var i:int=1; i<5; i++) {
				MovePanel["Move"+i].TNumber.text=myPokemon["Move"+i].PP+"MP";
				MovePanel["Move"+i].TName.text=myPokemon["Move"+i].Name;
			}
		}
		private function doMoveEvent(evt:MouseEvent) {
			doMove(evt.target.name,2);
		}
		private function enableMoves() {
			MovePanel.addEventListener(MouseEvent.CLICK,doMoveEvent);
		}
		private function disableMoves() {
			MovePanel.removeEventListener(MouseEvent.CLICK,doMoveEvent);
		}

		//---------------------ITEMS LIST
		private function loadItem() {
			while (ItemPanel.numChildren > 0) {
				ItemPanel.removeChildAt(0);
			}
			var countNumberOf:int=0;
			for (var i:int =0; i<itemList.length; i++) {
				itemList[i].y=235-24*i;
				itemList[i].reText();
				ItemPanel.addChild(itemList[i]);
			}
		}
		private function enableItem() {
			ItemPanel.mouseChildren=true;
			myOldHP=myPokemon.HP;
		}
		private function disableItem() {
			ItemPanel.mouseChildren=false;
		}
		public function AfterUsedItem() {
			loadItem();
			MyPokemonHP.text=String(myPokemon.HP);
			tweenList.unshift(new Tween (MyPokemonHPBar,"scaleX",Strong.easeOut,myOldHP/myPokemon.MaxHP,myPokemon.HP/myPokemon.MaxHP,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,endUseItem);
			disableItem();
		}
		private function endUseItem(e:Event) {
			showMovePanel();
			doMove("",0);
		}


		//---------------------POKEMON LIST 
		private function loadDelPokemonList() {			
			trace("loadDelPokemonList");
			var i:int;
			
			while (DelPokemonPanel.numChildren > 0) {
				DelPokemonPanel.removeChildAt(0);
			}
			hideAllPanel();
			for ( i=0; i<SQLData.pokemonList.length; i++) {
				var pokemon:PokemonInfor = new PokemonInfor(SQLData.pokemonList[i]);
				pokemon.y=24*i;
				pokemon.AddEventReplacePokemon(targetPokemon);
				DelPokemonPanel.addChild(pokemon);
			}
				
			var pokemonTargetInfor:PokemonInfor = new PokemonInfor(targetPokemon);
			DelPokemonPanel.addChild(pokemonTargetInfor);			
			DelPokemonPanel.addEventListener(MouseEvent.CLICK, clickDelPokemon);
		}
		private function clickDelPokemon(evt:MouseEvent) {
			if (evt.target.pokemon.HP!=undefined) {				
				removeBattle();		
			}
		}
//		private function loadPokemonList() {		
//			while (PokemonPanel.numChildren > 0) {
//				PokemonPanel.removeChildAt(0);			}
//			for (var i:int =0; i<SQLData.pokemonList.length; i++) {
//				//var newPokemonSwich:PokemonSwitch = new PokemonSwitch(SQLData.pokemonList[i],i);
//				//newPokemonSwich.y=24*i;
//				PokemonPanel.addChild(SQLData.pokemonList[i]);
//			}
//		}
		
		function loadPokemonList() {			
			while (PokemonPanel.numChildren > 0) {
				PokemonPanel.removeChildAt(0);
			}
			for (var i:int =0; i<SQLData.pokemonList.length; i++) {
				var pokemon:PokemonInfor = new PokemonInfor(SQLData.pokemonList[i]);
				pokemon.y=24*i;
				pokemon.AddEventSwitchPokemon();
				PokemonPanel.addChild(pokemon);
			}
		}
		private function enablePokemonSwitch() {
			SQLData.mainClip.Trace("enablePokemonSwitch");
			PokemonPanel.addEventListener(MouseEvent.CLICK, clickSwitchPokemon);
		}
		private function disablePokemonSwitch() {
			PokemonPanel.removeEventListener(MouseEvent.CLICK, clickSwitchPokemon);
		}
		private function clickSwitchPokemon(evt:MouseEvent) {
			loadPokemonList();
			if(myPokemonInList!=evt.target.pokemon.upid){				
				SQLData.mainClip.Trace("Switch Pokemon !");
				switchPokemon(evt.target.pokemon);
			}
		}
		private function switchPokemon(pokemonSwitch:PokemonObject) {
			hidePokemonPanel();
			showMovePanel();
			myPokemon=pokemonSwitch;
			loadPokemonBattle();
			if(manualSwitch){
				SQLData.mainClip.Trace("manualSwitch !");
				doMove("",0);
			}else{
				SQLData.mainClip.Trace("enableBattle !");
				enableBattle();
			}
		}
		private function endSwitchPokemon(e:Event) {
			doMove("",0);
		}
		//---------------------ATTACK
		private function doMove(myMoves:String,attackCase:int) {
			trace("--------------------------------");
			switch (attackCase) {
				case 3 :
					bothAttack(myMoves);
					break;
				case 2 :
					bothAttack(myMoves);
					break;
				case 1 :
					justMeAttack(myMoves);
					break;
				case 0 :
					justTargetAttack();
					break;
			}
		}
		private function moveTrapAndWeather(){
			switch (weather){
				case "Sun":				
					break;
				case "Rain":				
					break;
				case "Sand":				
					break;
				case "Hail":				
					break;
				case "Fog": // | |Defog				
					break;				
			}
			
//		private var weather:String="None";
//		private var weatherTurn:Number=-1;

		}
		private function bothAttack(myMoves:String) {
			//trace("bothAttack");
			var aiMove:String=targetPokemon.moveAI(myPokemon);
			if ((myPokemon.Speed>=targetPokemon.Speed && String(targetPokemon[aiMove].Effect.FA)=="") || String(myPokemon[myMoves].Effect.FA)!="" ) {
				doAttack(true,myPokemon,targetPokemon,myMoves,turn2TargetAttack);
			} else {
				doAttack(false,targetPokemon,myPokemon,aiMove,turn2MyAttack);
			}
			function turn2MyAttack(evt:Event) {
				justMeAttack(myMoves);
			}
			function turn2TargetAttack(evt:Event) {
				justTargetAttack();
			}
		}
		private function justMeAttack(myMoves:String) {
			doAttack(true,myPokemon,targetPokemon,myMoves,enableBattleEvent);//End Turn
		}
		private function justTargetAttack() {
			var aiMove:String=targetPokemon.moveAI(myPokemon);
			doAttack(false,targetPokemon,myPokemon,aiMove,enableBattleEvent);//End Turn
		}
		private function checkMyPokemonFailed(){
			enableBattle();
		}
		private function doAttack(isMe:Boolean,pokemonAttack:PokemonObject, pokemonDefense:PokemonObject, moveAttack:String, functionEnd:Function) {
			disableBattle();
			var i:int;
			var objectRef:Object=new Object();
			objectRef.iAttack=0;
			objectRef.iDefense=0;
			if (isMe) {
				objectRef.pokemonDefense="Target";
				objectRef.pokemonAttack="My";
				objectRef.xEffectStart=354;
				objectRef.xEffectEnd=544;
				objectRef.xTextAttack=350;
				objectRef.xTextDefense=580;
			} else {
				objectRef.pokemonAttack="Target";
				objectRef.pokemonDefense="My";
				objectRef.xEffectStart=743;
				objectRef.xEffectEnd=543;
				objectRef.xTextAttack=580;
				objectRef.xTextDefense=350;
			}
			if (pokemonAttack.HP>0) {
				pokemonAttack.OldHP =pokemonAttack.HP;
				if (pokemonAttack.isStatusEffect()) {
					//Selft Attack or ect.
					this[objectRef.pokemonAttack+"PokemonHP"].text=pokemonAttack.HP;
					tweenList.unshift(new Tween (getChildByName(objectRef.pokemonAttack+"PokemonHPBar"),"scaleX",Strong.easeOut,pokemonAttack.OldHP/pokemonAttack.MaxHP,pokemonAttack.HP/pokemonAttack.MaxHP,1,true));
					pokemonAttack.OldHP=pokemonAttack.HP;
					checkAbilitiStatus();////////////////////////////////								
					switch (pokemonAttack.Status) {
						case "Parlyz" :
						case "Sleep" :
						case "Frozen" :
							addChild(new FloatingText(pokemonAttack.Status+" | Cannot Attack!",objectRef.xTextAttack,175,1));
							endTweenEvent();
							break;
						case "Confuse" :
							addChild(new FloatingText("Selft Attack!",objectRef.xTextAttack,175,1));
							endTweenEvent();
							break;
						case "Burn" :
						case "Poison" :
							addChild(new FloatingText(pokemonAttack.Status+" | Lost HP!",objectRef.xTextAttack,175,1));
							tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,canAttackEvent);
							break;
					}
					checkMyPokemonFailed();
				} else {//!pokemonAttack.statusEffect()
					checkFlinching();
				}
			} else {//pokemonAttack.HP==0
				checkMyPokemonFailed();
			}
			

			function checkFlinching(){				
				TextField(getChildByName(objectRef.pokemonAttack+"PokemonName")).text=String(pokemonAttack.Name+" "+pokemonAttack.Status);
				if (pokemonAttack.Flinching) {
					pokemonAttack.Flinching=false;
					endTweenEvent();
				} else {
					canAttack();
				}				
			}
			function canAttackEvent(evt:Event) {
				canAttack();
			}
			function endTweenEvent() {
				tweenList.unshift(new Tween (MCFakeTween,"Alpha",Strong.easeOut,1,1,1,true));//Fake Event
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH, functionEnd);
			}
			function defChangeHPTween() {
				TextField(getChildByName(objectRef.pokemonDefense+"PokemonHP")).text=String(pokemonDefense.HP);
				tweenList.unshift(new Tween (getChildByName(objectRef.pokemonDefense+"PokemonHPBar"),"scaleX",Strong.easeOut,pokemonDefense.OldHP/pokemonDefense.MaxHP,pokemonDefense.HP/pokemonDefense.MaxHP,1,true));
			}
			function atkChangeHPTween() {
				TextField(getChildByName(objectRef.pokemonAttack+"PokemonHP")).text=String(pokemonAttack.HP);
				tweenList.unshift(new Tween (getChildByName(objectRef.pokemonAttack+"PokemonHPBar"),"scaleX",Strong.easeOut,pokemonAttack.OldHP/pokemonAttack.MaxHP,pokemonAttack.HP/pokemonAttack.MaxHP,1,true));
			}	
			function moreStatChange(Pokemon:PokemonObject,lStat:XMLList,refName:String) {
				var statAccuracy:Number = new Number(lStat.A);
				if (notMiss(statAccuracy)) {
					var listStatLength:Number=lStat.N.length();
					for (i=0; i<listStatLength; i++) {
						var statName:String= new String(lStat.N[i]);
						var statValue:Number= (new Number(lStat.F[i]));
						setStatChange(Pokemon,statName,statValue,refName)
					}
					defChangeHPTween();
				}
			}
			function setStatChange(Pokemon:PokemonObject,nStat:String,vStat:Number,sPokemon:String) {
				if(sPokemon=="Defense" && Pokemon.Ability=="Defiant" && vStat<0){
					Pokemon.moveChangeStat("Attack",5);
					addChild(new FloatingText(pokemonDefense.Ability+"+5 Attack",objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
				}
				var realValue:Number= Pokemon.moveChangeStat(nStat,vStat);
				if(realValue!=0)
				addChild(new FloatingText(realValue+" "+nStat,objectRef["xText"+sPokemon],115-(objectRef["i"+sPokemon]++)*floatSpace));
			}
			function setStatusEffect(Pokemon:PokemonObject,nStatus:String,aStatus:Number,sPokemon:String) {
				if (!((pokemonAttack.Ability=="Insomnia"||pokemonDefense.Ability=="Insomnia") && nStatus=="Poison")) {
					if (notMiss(aStatus) && Pokemon.Status=="") {
						Pokemon.setStatus(nStatus);
						TextField(getChildByName(objectRef["pokemon"+sPokemon]+"PokemonName")).text=String(Pokemon.Name+" "+Pokemon.Status);
						addChild(new FloatingText(nStatus,objectRef["xText"+sPokemon],115-(objectRef["i"+sPokemon]++)*floatSpace));
					}
				}
			}
			function setBattleWeather(){				
				
			}
			function checkAbilitiStatus() {
				switch (pokemonAttack.Ability) {
					case "ShedSkin" :
						if (notMiss(30)) {
							pokemonAttack.removeStatus();
							addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
							checkFlinching();
						}
						break;
					case "Synchronize" :
						if (pokemonAttack.Status=="Parlyz" || pokemonAttack.Status=="Burn" || pokemonAttack.Status=="Poison") {
							setStatusEffect(pokemonDefense,pokemonAttack.Status,100,"Defense");
							//pokemonAttack.removeStatus();
							addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
							checkFlinching();
						}
						break;
				}
			}
			function checkMoveSecondEffect(){
				
			}
			function dealDamage() {
				var damageReturn:Number=pokemonAttack.moveAttack(pokemonAttack[moveAttack],pokemonDefense);
				pokemonDefense.OldHP=pokemonDefense.HP;
				if(pokemonAttack[moveAttack].isCritical() && pokemonDefense.Ability!="ShellArmor"){
					damageReturn*=2;
					addChild(new FloatingText("Critical",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
				}
				if (String(pokemonAttack[moveAttack].Effect.XD)!="") {
					var rXD:Number=Math.round(Math.random()*( Number(pokemonAttack[moveAttack].Effect.XD)-1))+1;
					damageReturn*=rXD;
					trace(rXD+" xDamage -> "+damageReturn);
					addChild(new FloatingText("x"+rXD+ " Damage",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
				}
				if (String(pokemonAttack[moveAttack].Effect.KO)!="") {
					damageReturn=99999;
				}
				if (String(pokemonAttack[moveAttack].Effect.CHP)!="") {
					damageReturn=Number(pokemonAttack[moveAttack].Effect.CHP);
				}
				if (String(pokemonAttack[moveAttack].Effect.D0)!="") {
					damageReturn=0;
				}
				if (String(pokemonAttack[moveAttack].Effect.SD)!="") {
					pokemonAttack.HP=0;
					if(pokemonDefense.Ability=="Damp"){
						damageReturn=0;
					}
				}
				
				
				if (pokemonAttack.HP < pokemonAttack.MaxHP/3) {
					switch (pokemonAttack.Ability) {
						case "Overgrow" :
							if (pokemonAttack[moveAttack].Type=="GRASS") {
								addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
								damageReturn=Math.round(damageReturn*1.5);
							}
							break;
						case "Blaze" :
							if (pokemonAttack[moveAttack].Type=="FIRE") {
								addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
								damageReturn=Math.round(damageReturn*1.5);
							}
							break;
						case "Torrent" :
							if (pokemonAttack[moveAttack].Type=="WATER") {
								addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
								damageReturn=Math.round(damageReturn*1.5);
							}
							break;
						case "Swarm" :
							if (pokemonAttack[moveAttack].Type=="BUG") {
								addChild(new FloatingText(pokemonAttack.Ability,objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
								damageReturn=Math.round(damageReturn*1.5);
							}
							break;
					}
				}
				switch (pokemonAttack[moveAttack].Name){
					case "GyroBall":
					case "ElectroBall":
						damageReturn+=Math.round(10*(pokemonAttack.Speed/pokemonDefense.Speed));
						break
					case "CrushGrip":
						damageReturn+=Math.round(10*(pokemonDefense.MaxHP/pokemonDefense.HP));
						break
					case "Wake-upSlap":
						if(pokemonDefense.Status=="Sleep"){
							damageReturn*=2;
							pokemonDefense.removeStatus();							
						}
						break
					case "Brine":
						if(pokemonDefense.HP < pokemonDefense.MaxHP/2){
							damageReturn*=2;						
						}
						break
					case "MetalBurst":
						damageReturn=Math.round(pokemonDefense.Attack *1.5);			
						break
						
					case "Reversal":
					case "Flail":
						damageReturn+=Math.round(10*((pokemonAttack.MaxHP-pokemonAttack.HP)/20));
						break
					case "SuperFang":
						damageReturn=Math.round(pokemonDefense.HP/2);
						break
					case "WringOut":
					case "WaterSpout":					
						damageReturn+= -Math.round(10*((pokemonAttack.MaxHP-pokemonAttack.HP)/20));
						break
					case "FinalGambit":
						damageReturn=pokemonDefense.HP;
						pokemonAttack.HP=0;
						break
				}
				switch (pokemonDefense.Ability) {
					case "EffectSpore" :
						if (pokemonAttack[moveAttack].Category=="Physical" && pokemonAttack.Status=="") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							if (notMiss(90)) {
								var EffectSpore:Number=Math.round(Math.random()*2);
								switch (EffectSpore) {
									case 0 :
										setStatusEffect(pokemonAttack,"Poison",100,"Attack")
										break;
									case 1 :
										setStatusEffect(pokemonAttack,"Sleep",100,"Attack")
										break;
									case 2 :
										setStatusEffect(pokemonAttack,"Parlyz",100,"Attack")
										break;
								}
							}
						}
						break;
					case "Justified" :
						if (pokemonAttack[moveAttack].Type=="DARK") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonDefense.setNewTemStat("Attack",pokemonDefense.Attack*1.1);
						}
						break;
					case "Heatproof" :
						if (pokemonAttack[moveAttack].Type=="FIRE") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							damageReturn=Math.round(damageReturn/2);
						}
						break;
					case "Levitate" :
						if (pokemonAttack[moveAttack].Type=="GROUND" || pokemonAttack[moveAttack].Name=="SkyDrop"|| pokemonAttack[moveAttack].Name=="Spikes") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							damageReturn*=0;
						}
						break;
					case "ThickFat" :
						if (pokemonAttack[moveAttack].Type=="FIRE" || pokemonAttack[moveAttack].Type=="ICE") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							damageReturn=Math.round(damageReturn/2);
						}
						break;

					case "FlashFire" :
						if (pokemonAttack[moveAttack].Type=="FIRE") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							damageReturn=0;
							pokemonDefense.addBonusMoveAtkByAbility("FIRE",1.5);
						}
						break;
					case "WeakArmor" :
						if (pokemonAttack[moveAttack].Category=="Physical") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonDefense.setNewTemStat("Defense",pokemonDefense.Defense*0.9);
							pokemonDefense.setNewTemStat("Speed",pokemonDefense.Speed*1.1);
						}
					case "Lightningrod" :
						if (pokemonAttack[moveAttack].Type=="FIGHTING") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonDefense.setNewTemStat("SpAtk",pokemonDefense.SpAtk*1.1);
							damageReturn=0;
						}
					case "FlameBody" :
						if (pokemonAttack[moveAttack].Category=="Physical") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonAttack.setStatus("Burn");
						}
						break;
					case "Aftermath" :
						if (pokemonAttack[moveAttack].Category=="Physical") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonAttack.HP-=Math.round(pokemonAttack.MaxHP/4);
						}
					case "WaterAbsorb" :
						if (pokemonAttack[moveAttack].Type=="WATER") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonAttack.HP-=Math.round(pokemonAttack.MaxHP/4);
						}
						break;
					case "DrySkin" :
						if (pokemonAttack[moveAttack].Type=="WATER") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonDefense.HP+=Math.round(pokemonDefense.MaxHP/4);
						}
						if (pokemonAttack[moveAttack].Type=="FIRE") {
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							damageReturn=Math.round(damageReturn*1.25);
						}
						break;
				}
				pokemonDefense.HP-=damageReturn;
				pokemonDefense.set0HP();
				pokemonDefense.setMaxHP();
				if(pokemonDefense.Ability=="Sturdy"){
					if(pokemonDefense.HP==0){
						if(pokemonDefense.OldHP==pokemonDefense.MaxHP){
							addChild(new FloatingText(pokemonDefense.Ability,objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
							pokemonDefense.HP=1;
						}
					}
					
				}
				if (String(pokemonAttack[moveAttack].Effect.RC)!="") {
					pokemonAttack.HP-=Math.round(damageReturn*Number(pokemonAttack[moveAttack].Effect.RC/100));
					addChild(new FloatingText("Recoil",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
				}
				if (String(pokemonAttack[moveAttack].Effect.LS)!="") {
					var lifeStealh:Number=Math.round(damageReturn*Number(pokemonAttack[moveAttack].Effect.LS));
					trace(lifeStealh+"lifeStealh");
					var realValue:Number= pokemonAttack.moveChangeStat("HP",lifeStealh);
					addChild(new FloatingText("Recovers "+realValue+"HP",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
				}
				addChild(new FloatingText(String(damageReturn),objectRef.xTextDefense,175,1));
				tweenList.unshift(new Tween (getChildByName(objectRef.pokemonDefense+"Effect"),"alpha",Strong.easeOut,1,0,1,true));
				tweenList.unshift(new Tween (getChildByName(objectRef.pokemonDefense+"Effect"),"x",Strong.easeOut,objectRef.xEffectStart,objectRef.xEffectEnd,1,true));
				defChangeHPTween();
				pokemonAttack.set0HP();
				pokemonDefense.setMaxHP();
				atkChangeHPTween();
				
			}
			function notMiss(Accuracy:Number):Boolean {
				if (Math.round(Math.random()*100) > Accuracy) {
					return false;
				} else {
					return true;
				}
			}
			function canAttack() {
				trace("" +pokemonAttack.Name);
				if (pokemonAttack.HP>0) {//Check Status And Self Attack may be 0HP
					if (pokemonAttack.AttackTurn==0) {//MoreTurn  && String(pokemonAttack[moveAttack].Effect.MT)!=""
						var PressureAbility:Number=pokemonAttack[moveAttack].PP;
						if(pokemonDefense.Ability=="Pressure"){
							PressureAbility*=2;
						}
						if (Number(pokemonAttack.Total)<PressureAbility) {// PP
							pokemonAttack.OldHP=pokemonAttack.HP;
							pokemonAttack.HP-=PressureAbility;
							atkChangeHPTween();
						} else {
							pokemonAttack.Total-=PressureAbility;
							TextField(getChildByName(objectRef.pokemonAttack+"PokemonTotal")).text=String(pokemonAttack.Total);
						}
						if (String(pokemonAttack[moveAttack].Effect.AH)!="" || (notMiss(pokemonAttack[moveAttack].Accuracy) && notMiss(pokemonAttack.Accuracy) && !notMiss(pokemonDefense.Evasive))) {
							//2T, FL
							addChild(new FloatingText(pokemonAttack[moveAttack].Name,objectRef.xTextAttack,175,1));
							if (String(pokemonAttack[moveAttack].Effect.S)!="") {
								moreStatChange(pokemonAttack,pokemonAttack[moveAttack].Effect.S,"Attack");
							}
							if (String(pokemonAttack[moveAttack].Effect.O)!="") {
								if (!(pokemonAttack[moveAttack].Attack>1 && pokemonDefense.Ability=="ShieldDust")) {									
									moreStatChange(pokemonDefense,pokemonAttack[moveAttack].Effect.O,"Defense");
								}
							}
							if (String(pokemonAttack[moveAttack].Effect.SS)!="") {
								setStatusEffect(pokemonAttack,String(pokemonAttack[moveAttack].Effect.SS.N),Number(pokemonAttack[moveAttack].Effect.SS.A),"Attack");
							}
							if (String(pokemonAttack[moveAttack].Effect.OS)!="") {
								setStatusEffect(pokemonDefense,String(pokemonAttack[moveAttack].Effect.OS.N),Number(pokemonAttack[moveAttack].Effect.OS.A),"Defense");
							}
							if (String(pokemonAttack[moveAttack].Effect.WT)!="") {
								setBattleWeather();
							}
							if (String(pokemonAttack[moveAttack].Effect.MT)!="") {
								pokemonAttack.CurentMove=moveAttack;
								pokemonAttack.AttackTurn=Number(pokemonAttack[moveAttack].Effect.MT.T);
								pokemonAttack.AttackTurnBonus=Number(pokemonAttack[moveAttack].Effect.MT.F);
								pokemonAttack.AttackFunction="MT";
							}
							switch(pokemonAttack[moveAttack].Name){
								case "Aromatherapy":
								trace(pokemonAttack[moveAttack].Name);
									pokemonAttack.removeStatus();								
									break;
								case "ChipAway":
								trace(pokemonAttack[moveAttack].Name);
								case "ClearSmog":
								trace(pokemonAttack[moveAttack].Name);
									pokemonDefense.recoveryStatByMax();								
									break;
								case "Haze":
								trace(pokemonAttack[moveAttack].Name);
									pokemonAttack.recoveryStatByMax();								
									break;
								case "BellyDrum":
								trace(pokemonAttack[moveAttack].Name);
									pokemonAttack.setNewTemStat("Attack",pokemonAttack.TemAttack*2);
									pokemonAttack.HP-=Math.round(pokemonAttack.MaxHP/2);								
									break;
								case "PainSplit":
								trace(pokemonAttack[moveAttack].Name);
									var arg:Number=Math.round((pokemonAttack.HP+pokemonDefense.HP)/2);		
									pokemonDefense.HP=arg;				
									pokemonAttack.HP=arg;
									break;
								case "Endeavor":	
								trace(pokemonAttack[moveAttack].Name);								
									pokemonDefense.HP=pokemonAttack.HP;						
									break;
								default:
								
								
							}
							pokemonAttack.set0HP();
							pokemonDefense.set0HP();
							pokemonAttack.setMaxHP()
							pokemonDefense.setMaxHP();
							if (String(pokemonAttack[moveAttack].Effect.FL)!="" && pokemonDefense.Ability!="InnerFocus") {
								if (notMiss(Number(pokemonAttack[moveAttack].Effect.FL))) {
									addChild(new FloatingText("Flinch",objectRef.xTextDefense,115-(objectRef.iDefense++)*floatSpace));
									pokemonDefense.Flinching=true;
									pokemonDefense.CurentMove="";
									pokemonDefense.AttackTurn=0;
								}
							}
							if (pokemonAttack[moveAttack].Attack>1) {//Deal Damage
								if (String(pokemonAttack[moveAttack].Effect.T2)!="") {
									pokemonAttack.AttackFunction="T2";
									pokemonAttack.AttackTurn=1;
									pokemonAttack.CurentMove=moveAttack;
									addChild(new FloatingText("Turn 1",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
								} else {
									if (String(pokemonAttack[moveAttack].Effect.T1)!="") {
										pokemonAttack.AttackFunction="T1";
										pokemonAttack.AttackTurn=1;
										addChild(new FloatingText("Turn 1",objectRef.xTextAttack,115-(objectRef.iAttack++)*floatSpace));
									}
									dealDamage();
									
								}
							}
							defChangeHPTween();
							atkChangeHPTween();
							endTweenEvent();
						} else {
							addChild(new FloatingText("Miss",objectRef.xTextAttack,175,1));
							endTweenEvent();
						}
					} else {//end 2Turn
						moveAttack=pokemonAttack.CurentMove;
						switch (pokemonAttack.AttackFunction) {
							case "T2" :
								moveAttack=pokemonAttack.CurentMove;
								addChild(new FloatingText(pokemonAttack[moveAttack].Name,objectRef.xTextAttack,175,1));
								dealDamage();
								break;
							case "T1" :
								addChild(new FloatingText("Can't Attack",objectRef.xTextAttack,175,1));
								break;
							case "MT" :
								addChild(new FloatingText(pokemonAttack[moveAttack].Name,objectRef.xTextAttack,175,1));
								pokemonAttack.AttackTurnBonus*=2;
								dealDamage();
								break;
						}
						pokemonAttack.AttackTurn--;
						if (pokemonAttack.AttackTurn==0) {
							if (pokemonAttack.AttackFunction=="MT") {
								setStatusEffect(pokemonAttack,String(pokemonAttack[moveAttack].Effect.MT.N),Number(pokemonAttack[moveAttack].Effect.MT.A),"Attack");
							}
							pokemonAttack.AttackFunction="";
							pokemonAttack.AttackTurnBonus=0;
						}
						weatherStatEffect();
						endTweenEvent();	
						
					}
				} else {//End Check 					
					checkMyPokemonFailed();
				}
			}
		}
		private function disableThrow() {
			if(!isTrainer){
				Pokeball.removeEventListener(MouseEvent.CLICK,throwPokeball);
				Greatball.removeEventListener(MouseEvent.CLICK,throwGreatball);
				Ultraball.removeEventListener(MouseEvent.CLICK,throwUltraball);
			}

		}
		private function enableThrow() {
			if(!isTrainer){
//				Pokeball.alpha=1;
//				Greatball.alpha=1;
//				Ultraball.alpha=1;
				Pokeball.addEventListener(MouseEvent.CLICK,throwPokeball);
				Greatball.addEventListener(MouseEvent.CLICK,throwGreatball);
				Ultraball.addEventListener(MouseEvent.CLICK,throwUltraball);
			}else{
				Pokeball.visible=false;
				Ultraball.visible=false;
				Greatball.visible=false;
			}
		}
		private function throwPokeball(evt:MouseEvent) {
			if(SQLData.changePMP(-5))
				throwBall("pokeball");
		}
		private function throwGreatball(evt:MouseEvent) {
			if(SQLData.changePMP(-10))
				throwBall("greatball");
		}
		private function throwUltraball(evt:MouseEvent) {
			if(SQLData.changePMP(-15))
				throwBall("ultraball");
		}
		private function throwBall(ballName:String) {
			var temRate:Number=targetPokemon.Rate;
			var HPRate:Number=targetPokemon.HP/targetPokemon.MaxHP;
			if(HPRate < 1/8) 				temRate-=3;
			else if(HPRate < 1/4) 			temRate-=2;
			else if(HPRate < 1/2) 			temRate-=1;
			if(ballName=="ultraball")		temRate-=2;
			else if(ballName=="greatball")	temRate--;
			if(targetPokemon.Status=="Sleep" || targetPokemon.Status=="Frozen")temRate--;
			if(temRate<1)temRate=1;
			//temRate=0;
			var catchNumber:Number=Math.round(Math.random()*temRate);
			if(catchNumber==1)catched=true;
			else catched=false;
			Balls.gotoAndStop(ballName);
			disableThrow(); 
			disableBattle();
			tweenList.unshift(new Tween (Balls,"alpha",Strong.easeOut,0,1,1,true));
			tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,430,700,1,true));
			tweenList.unshift(new Tween (Balls,"y",Strong.easeOut,220,190,1,true));
			tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballOpen);
			function ballOpen(e:Event){				
				tweenList.unshift(new Tween (LoadPokemonImage,"alpha",Strong.easeOut,1,0,0.5,true));
				tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,700,650,1,true));
				tweenList.unshift(new Tween (Balls,"scaleX",Strong.easeOut,1,4,0.5,true));
				tweenList.unshift(new Tween (Balls,"scaleY",Strong.easeOut,1,4,0.5,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballClose);
				
			}
			function ballClose(e:Event){				
				tweenList.unshift(new Tween (Balls,"scaleY",Strong.easeOut,4,1,0.5,true));
				tweenList.unshift(new Tween (Balls,"scaleX",Strong.easeOut,4,1,0.5,true));
				tweenList.unshift(new Tween (Balls,"y",Strong.easeOut,190,245,1,true));
				tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,650,700,1,true));
				tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,ballCheck);
			}
				
			function ballCheck(e:Event){				
			catched=true;
				if(catched){
					tweenList.unshift(new Tween (Balls,"x",Strong.easeOut,700,430,1,true));							
					disableBattle();		
					if(SQLData.pokemonList.length<6){							
						SQLData.pokemonList.push(targetPokemon);
						SQLData.InsertPokemonSQL(targetPokemon);
						removeBattle();
					}else{
						addChild(new FloatingText("No More Than 6 Pokemon, Remove One!",310,155,2));
						loadDelPokemonList();
					}
				
				}else{
					LoadPokemonImage.alpha=1;
					justTargetAttack();
				}
				tweenList.unshift(new Tween (Balls,"alpha",Strong.easeOut,1,0,1,true));
				
			}
		}
	}
}